package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;

import com.itheima.utils.HealthStringUtils;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description: 会员controller层
 * @author: wangjingliang
 * @create: 2019-11-20 19:51
 **/
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * url:post("/member/login.do",this.loginInfo
     * @param: Map
     * @return: Result
     * @date: 2019/11/20
     */
    @PostMapping("/login")
    public Result login(@RequestBody Map map, HttpServletResponse response){
        //判断手机号和验证码是否存在
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        if (HealthStringUtils.isEmpty(telephone)||HealthStringUtils.isEmpty(validateCode)){
            return  new Result(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        //然后根据手机号获取验证码
        String validateCode4String = null;

            validateCode4String = (String) redisTemplate.opsForValue().get(telephone+RedisMessageConstant.SENDTYPE_LOGIN);

        //对获取的验证码进行判断
        //为空则说明:验证码已经过期了
        if (HealthStringUtils.isEmpty(validateCode4String)) {
            return new Result(false,"验证码未发生或已过期");
        }
        //有值,则进行判断验证码是否匹配
        if (!validateCode.equalsIgnoreCase(validateCode4String)){
            return  new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        //将手机号传入后台并做处理返回密码
        Member member =null;
        try {
            member =memberService.login(telephone);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, e.getMessage());
        }

        //对数据进行储存到cookie中
        Cookie cookie = new Cookie("memberLoginTelephone",telephone);
        cookie.setPath("/");
        cookie.setMaxAge(60*60*24*30);
        response.addCookie(cookie);

        //并且将它存储到redis中
        //先将返回的member会员对象转换成json
        String memberJson = JSON.toJSON(member).toString();

            redisTemplate.opsForValue().set(telephone,memberJson,60*60*24*30, TimeUnit.SECONDS);

        return  new Result(true,MessageConstant.LOGIN_SUCCESS);

    }
}
